var utils = {
  parse: function (responseText) {
    try {
      return responseText ? JSON.parse(responseText) : {};
    } catch (e) {
      return {};
    }
  },

  getQueryString: function (name) {
    var result = location.search.match(
      new RegExp('[?&]' + name + '=([^&]+)', 'i')
    );
    if (!result || result.length < 1) {
      return '';
    }
    return decodeURIComponent(result[1]);
  },

  getQueryInt: function(name) {
    var value = utils.getQueryString(name);
    return value ? parseInt(value) : 0;
  },

  getPageUrl: function(fileName) {
    var url = fileName +
      '?pluginId=' + utils.getQueryString('pluginId') +
      '&apiUrl=' + encodeURIComponent(utils.getQueryString('apiUrl'));
    var siteId = utils.getQueryInt('siteId');
    var channelId = utils.getQueryInt('channelId');
    var contentId = utils.getQueryInt('contentId');
    if (siteId > 0) url += '&siteId=' + siteId;
    if (channelId > 0) url += '&channelId=' + channelId;
    if (contentId > 0) url += '&contentId=' + contentId;
    return url;
  },

  getPageAlert: function (error) {
    var message = error.message;
    if (error.response && error.response.data) {
      if (error.response.data.exceptionMessage) {
        message = error.response.data.exceptionMessage;
      } else if (error.response.data.message) {
        message = error.response.data.message;
      }
    }

    return {
      type: "danger",
      html: message
    };
  },

  loading: function (isLoading) {
    if (isLoading) {
      return layer.load(1, {
        shade: [0.2, '#000']
      });
    } else {
      layer.close(layer.index);
    }
  },

  up: function () {
    document.documentElement.scrollTop = document.body.scrollTop = 0;
  },

  closeLayer: function () {
    parent.layer.closeAll();
    return false;
  },

  openLayer: function (config) {
    if (!config || !config.url) return false;

    if (!config.width) {
      config.width = $(window).width() - 50;
    }
    if (!config.height) {
      config.height = $(window).height() - 50;
    }

    if (config.full) {
      config.width = $(window).width() - 50;
      config.height = $(window).height() - 50;
    }

    layer.open({
      type: 2,
      btn: null,
      title: config.title,
      area: [config.width + 'px', config.height + 'px'],
      maxmin: true,
      resize: true,
      shadeClose: true,
      content: config.url
    });

    return false;
    },

    alertDelete: function (config) {
        if (!config) return false;

        swal({
            title: config.title,
            text: config.text,
            type: 'question',
            confirmButtonText: '确认删除',
            confirmButtonClass: 'btn btn-danger',
            showCancelButton: true,
            cancelButtonText: '取 消',
            cancelButtonClass: 'btn btn-default ml-3',
            buttonsStyling: false,
        }).then(function (result) {
            if (result.value) {
                config.callback();
            }
        });

        return false;
    },

    openImagesLayer: function (imageUrls) {
        var data = [];
        for (var i = 0; i < imageUrls.length; i++) {
          var imageUrl = imageUrls[i];
          data.push({
            src: imageUrl, //原图地址
            thumb: imageUrl //缩略图地址
          });
        }
        layer.photos({
          photos: {
            data: data
          },
          anim: 5
        });
    },

    IdCard: function(UUserCard, num) {
        if (num == 1) {
            //获取出生日期
            birth = UUserCard.substring(6, 10) + "-" + UUserCard.substring(10, 12) + "-" + UUserCard.substring(12, 14);
            return birth;
        }
        if (num == 2) {
            //获取性别
            if (parseInt(UUserCard.substr(16, 1)) % 2 == 1) {
                //男
                return "男";
            } else {
                //女
                return "女";
            }
        }
        if (num == 3) {
            //获取年龄
            var myDate = new Date();
            var month = myDate.getMonth() + 1;
            var day = myDate.getDate();
            var age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1;
            if (UUserCard.substring(10, 12) < month || UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <= day) {
                age++;
            }
            return age;
        }
    },

    exportTableToExcel: function (tableId, titleId) {
        /* generate workbook object from table */
        var wb = XLSX.utils.table_to_book(document.getElementById(tableId));
        /* generate file and force a download*/
        XLSX.writeFile(wb, document.getElementById(titleId).innerHTML.replace(/\s+/g, "") + ".xlsx");
    },

    exportJsonToExcel: function (jsonBook, fileName) {

        console.log(jsonBook);

        var wb = XLSX.utils.book_new();
        var ws;

        for (var i in jsonBook) {
            if (jsonBook[i].value.length > 0) {
                ws = XLSX.utils.json_to_sheet(jsonBook[i].value);
                /* add worksheet to workbook */
                XLSX.utils.book_append_sheet(wb, ws, jsonBook[i].title);
            }
        }
        
        
        /* write workbook */
        XLSX.writeFile(wb, fileName);
    }
};

var $api = axios.create({
    baseURL: utils.getQueryString('apiUrl') + '/SS.LNDX.Service/',
    withCredentials: true
});
